package com.runrunfast.fxj.config;

/**
 * @author: 陈胤训
 * @create: 2018-11-01 18:56
 **/


import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解决跨域
 */
@Configuration
public class SpringWebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/192.168.0.110", buildConfig());
        return new CorsFilter(source);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    /**  增加拦截器 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       // registry.addInterceptor(new HttpInterceptor());
    }


   /* *//** 过滤器bean注册  *//*
    @Bean
    public FilterRegistrationBean getDemoFilter(){
        LoginFilter demoFilter=new LoginFilter();
        FilterRegistrationBean registrationBean=new FilterRegistrationBean();
        registrationBean.setFilter(demoFilter);
        List<String> urlPatterns=new ArrayList<>();
        urlPatterns.add("/admin/*");//拦截路径，可以添加多个
        urlPatterns.add("/sys/*");//拦截路径，可以添加多个
        registrationBean.setUrlPatterns(urlPatterns);
        registrationBean.setOrder(1);
        return registrationBean;
    }

    *//** 过滤器bean注册  *//*
    @Bean
    public FilterRegistrationBean getDemoFilter1(){
        AclControlFilter demoFilter=new AclControlFilter();
        FilterRegistrationBean registrationBean=new FilterRegistrationBean();
        registrationBean.setFilter(demoFilter);
        List<String> urlPatterns=new ArrayList<>();
        urlPatterns.add("/admin/*");//拦截路径，可以添加多个
        urlPatterns.add("/sys/*");//拦截路径，可以添加多个
        registrationBean.setUrlPatterns(urlPatterns);
        registrationBean.setOrder(2);
        Map<String ,Object> map = new HashMap<>() ;
        map.put("exclusionUrls","/sys/user/noAuth,/login1");
        //map.put("targetFilterLifecycle",true);
        registrationBean.setInitParameters(map);
        return registrationBean;
    }*/

}